package com.TT.ticket.threadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PoolDemo {
    public static void main(String[] args) {
        //ExecutorService executorService = Executors.newSingleThreadExecutor(); //单个线程
        //ExecutorService executorService = Executors.newFixedThreadPool(5); //创建一个固定的线程池大小
        ExecutorService executorService = Executors.newCachedThreadPool(); //可伸缩的，遇强则强，遇弱则弱


        try {
            for (int i = 0; i < 10; i++) {
                //使用了线程池之后，使用线程池来创建线程
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok ");
                });

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            //线程池用完，线程结束，关闭线程池
            executorService.shutdown();

        }

    }
}
